// pages/memberCenter/index.js
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIndex: 0,
    scrollHeight:0,
    //积分
    integralList: [],
    integralLoadMore: true, // 没有更多数据
    isLoading: true, // 是否正在加载中
    integralPage: '1',
    integralSize: '20',
    couponList: [],
    couponIsLoading: true,
    couponLoadMore:true,
    couponPage: '1',
    couponSize: '20'
  },
  onLoad(options){
    this.setData({
      navIndex:options.type=='jf' ? 1:0
    })
    this.getIntegralList()
    this.setListHeight()
    this.getCouponList()
  },
  // 切换导航
  handleNav(e) {
    this.setData({
      navIndex: e.currentTarget.dataset.index
    })
  },
  // 获取积分
  getIntegralList(){
    return new Promise((resolve,reject)=>{
      let PageIndex = this.data.integralPage.toString()
      let PageSize = this.data.integralSize
      App._post_form('User/GetOrderList', {PageIndex, PageSize}, (res) => {
        let itemList=res.items
        this.setData({
          integralList: this.data.integralList.concat(itemList),
          isLoading: false
        })
        if(itemList.length<this.data.integralSize){
          this.setData({
            integralLoadMore: false
          })
        }
        resolve()
      });
    })
  },
  // 获取优惠券
  getCouponList(){
    return new Promise((resolve,reject)=>{
      let PageIndex = this.data.couponPage.toString()
      let PageSize = this.data.couponSize
      App._post_form('User/Api010', {PageIndex, PageSize}, (res) => {
        let itemList=res.data.items.map(item=>{
          item.price=parseFloat(item.price).toFixed(0)
          return item
        })
        this.setData({
          couponList: this.data.couponList.concat(itemList),
          couponIsLoading: false
        })
        if(itemList.length<this.data.couponSize){
          this.setData({
            couponLoadMore: false
          })
        }
        resolve()
      });
    })
  },
  /**
   * 上拉到底加载数据
   */
  bindUpLoad() {
    if (this.data.integralLoadMore) {
      this.setData({
        integralPage: this.data.integralPage*1 + 1
      }, () => {
        this.getIntegralList();
      })
    }
  },
  //优惠券上拉加载
  couponBindUpLoad() {
    if (this.data.couponLoadMore) {
      this.setData({
        couponPage: this.data.couponPage*1 + 1
      }, () => {
        this.getCouponList();
      })
    }
  },
  /**
   * 设置分类列表高度
   */
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 55,
        });
      }
    });
  },
})